<?php

namespace App\Http\Services;

/*赠送记录*/
use App\Http\Model\GiftBagRechargeLog;

class GiftBagRechargeLogServices extends CommonServices
{


    public function __construct()
    {
        $this->giftBagRechargeLog = new GiftBagRechargeLog();
    }

    /*
     * 添加赠送记录
     * */
    public function createRechargeLog($user_id, $rank, $diamond_num, $pearl_rand)
    {

        $res = $this->giftBagRechargeLog->createLog($user_id, $rank, $diamond_num, $pearl_rand);
        if (!$res) {
            write_log('giftBagConsumer', 'RechargeLogError', json_encode($user_id . '-' . $rank));//重复充值等级
            return false;
        }
        return $res;
    }


    /*
    * 查看是否已经有充值记录
    * @param [$user_id]   用户id
    * @param [$config_date]   配置活动时间
    * @param [$timestamp]   充值时间
    * @param [$rank]   充值等级
    * */
    public function sendLog($user_id, $rank)
    {
        $res = $this->giftBagRechargeLog->dayLog($user_id, $rank);
        if (!$res) {
            return true; //当天没有该充值类型记录
        }
        return false;
    }

    public function giftBagRechargeLog($user_id)
    {
        $rank_arr = [1 => 0, 2 => 0, 3 => 0];
        $log = $this->giftBagRechargeLog->userGiftBagLog($user_id);
        if ($log) {
            $rank = array_column($log, 'rank');
            foreach ($rank as $v) {
                $rank_arr[$v] = 1;
            }
        }
        return $rank_arr;
    }

    //点击领取
    public function giftBagClickGet($user_id, $rank)
    {
        $res = $this->giftBagRechargeLog->dayLog($user_id, $rank);
        $code = 200;//
        do {
            if (!$res) {
                $code = 201;//未充值该等级
                break;
            }
            if ($res->status == 1) {
                $code = 202;//该礼包已领取
                break;
            }
            $updateRank = $this->giftBagRechargeLog->updateLog($user_id, $rank,$res->award_pearl);
            if (!$updateRank) {
                $code = 203;//领取失败
                break;
            }
        } while (false);
        return $code;
    }

}